package com.hsoft.zhxy.app.xtgl_02_01.controller;

import com.alibaba.fastjson.JSONObject;
import com.hsoft.zhxy.app.common.model.entity.XtcsglEntity;
import com.hsoft.zhxy.app.xtgl_02_01.service.XtcsglService;
import com.hsoft.zhxy.common.result.ResultBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.io.IOException;


@Api(tags = "xtgl_02_01 系统参数管理")
@Log4j2
@Validated
@RestController
@RequestMapping(value = "/xtgl/xtcsgl/", produces = "application/json;charset=UTF-8")
public class XtcsglController {

    @Autowired
    XtcsglService xtcsglService;

    @ApiOperation(value = "查询单条数据", notes = "查询单条数据")
    @GetMapping("findOne")
    public JSONObject findOne(@Valid XtcsglEntity xtcsglEntity) {
        return xtcsglService.findOne(xtcsglEntity);
    }

    @ApiOperation(value = "查询列表数据", notes = "查询列表数据")
    @GetMapping("findList")
    public JSONObject findList(XtcsglEntity xtcsglEntity) {
        return xtcsglService.findList(xtcsglEntity);
    }

    @ApiOperation(value = "查询分页列表数据", notes = "查询分页列表数据")
    @GetMapping("findListByPage")
    public JSONObject findListByPage(XtcsglEntity xtcsglEntity, Long page, Long pageSize) {
        return xtcsglService.findListByPage(xtcsglEntity, page, pageSize);
    }

    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping("add")
    public JSONObject add(@RequestBody @Valid XtcsglEntity xtcsglEntity) {
        return xtcsglService.add(xtcsglEntity);
    }

    @ApiOperation(value = "修改", notes = "修改")
    @PostMapping("update")
    public JSONObject update(@RequestBody @Valid XtcsglEntity xtcsglEntity) {
        return xtcsglService.update(xtcsglEntity);
    }

    @ApiOperation(value = "删除", notes = "删除")
    @PostMapping("delete")
    public JSONObject delete(@RequestBody XtcsglEntity xtcsglEntity) {
        return xtcsglService.delete(xtcsglEntity);
    }

    @ApiOperation(value = "上传", notes = "上传")
    @PostMapping("upload")
    public JSONObject upload(@RequestParam MultipartFile excel) {
        String filename = excel.getOriginalFilename();
        File f = new File("D:\\dtgl");
        try {
            excel.transferTo(f);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResultBody.setData(filename);
    }

}
